﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SocketClient
{
    public partial class MainForm : Form
    {

        private ConnServer conn;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.cboType.SelectedIndex = 0;
        }

        private void Thread_conn(object obj)
        {
            string[] array = (string[])obj;
            this.conn = new ConnServer(array[0], int.Parse(array[1]), array[2]);
            this.conn.Connect();
        }


        private void BtnConn_Click(object sender, EventArgs e)
        {
            if (!(this.btnConn.Text == "连接"))
            {
                if (this.btnConn.Text == "断开")
                {
                    this.conn.CloseConnect();
                    this.btnConn.Text = "连接";
                }
                return;
            }
            this.btnConn.Enabled = false;
            this.btnConn.Text = "连接中.";
            Thread thread = new Thread(new ParameterizedThreadStart(this.Thread_conn));
            thread.IsBackground = true;
            if (this.cboType.SelectedIndex == 1)
            {
                thread.Start(new string[]
                {
                    this.txtIP.Text,
                    this.txtPort.Text,
                    this.cboType.Text
                });
                return;
            }
            thread.Start(new string[]
            {
                this.txtWS.Text.Trim(),
                "0",
                this.cboType.Text
            });
        }

        private void BtnSend_Click(object sender, EventArgs e)
        {
            this.conn.SSend(this.txtMsg.Text.Trim());
        }

        private void CboType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cboType.SelectedIndex == 0)
            {
                this.panel.Visible = false;
            }
            else
            {
                this.panel.Visible = true;
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Process.GetCurrentProcess().Kill();
            //Environment.Exit(0);
        }

        public void AddLog(string text)
        {
            if (this.rtxtMsg.InvokeRequired)
            {
                this.rtxtMsg.Invoke(new Action<string>(AddLog), text);
            }
            else
            {
                this.rtxtMsg.AppendText(text + Environment.NewLine);
                this.rtxtMsg.SelectionStart = this.rtxtMsg.TextLength;
                this.rtxtMsg.ScrollToCaret();
            }
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            string text = null;
            ConnServer.que_msg.TryDequeue(out text);
            if (text != null)
            {
                if (!text.Contains("@heart"))
                {
                    AddLog(text);
                }
                if (text.Contains("连接成功！"))
                {
                    this.btnConn.Enabled = true;
                    this.btnConn.Text = "断开";
                    this.btnSend.Enabled = true;
                    return;
                }
                if (text.Contains("连接失败！") || text.Contains("连接断开！"))
                {
                    this.btnConn.Enabled = true;
                    this.btnConn.Text = "连接";
                    this.btnSend.Enabled = false;
                }
            }
        }
    }
}
